<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="计收年度" prop="year">
        <el-date-picker
          v-model="queryParams.year"
          type="year"
          class="w202"
          format="yyyy"
          value-format="yyyy"
          placeholder="选择计收年度">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="水管机构" prop="orgCd" width="50px">
        <treeselect
          v-model="queryParams.orgCd"
          :options="xzqhOptions"
          :disable-branch-nodes="true"
          :normalizer="normalizer"
          :show-count="true"
          placeholder="水管机构"
          @select="selectAd"
        />
      </el-form-item>
      <el-form-item v-if="feeKind == 0" label="农户" prop="farmCd">
        <el-select @change="$forceUpdate()" v-model="queryParams.farmCd">
              <el-option
                  v-for="item in WiFarmerBList"
                  :key="item.farmCd"
                  :label="item.farmNm"
                  :value="item.farmCd"
              ></el-option>
          </el-select>
      </el-form-item>
      <!-- <el-form-item label="核算人" prop="operId">
        <el-input
          v-model="queryParams.operId"
          placeholder="请输入核算人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="核算时间" prop="operTime">
        <el-date-picker clearable
          v-model="queryParams.operTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择核算时间">
        </el-date-picker>
      </el-form-item> -->

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="getList">查询</el-button>
        <el-button type="primary" plain size="mini" :disabled="WiOrgfeeYList.length>0" @click="caleFee">核算</el-button>
        <!-- <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button> -->
      </el-form-item>
    </el-form>

    <!-- <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['waterPrice:WiOrgfeeY:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['waterPrice:WiOrgfeeY:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['waterPrice:WiOrgfeeY:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['waterPrice:WiOrgfeeY:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row> -->

    <el-table v-loading="loading" :data="WiOrgfeeYList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="计收年度" align="center" prop="feeYr" />
      <!-- <el-table-column label="水管机构" align="center" prop="orgNm" /> -->
      <el-table-column label="水管机构" v-if="feeKind == 1" align="left" prop="orgNm" />
      <el-table-column label="农户" v-else align="left" prop="farmNm" />
      <!-- <el-table-column label="地块性质" align="center" prop="landAttr">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.land_attr" :value="scope.row.landAttr"/>
        </template>
      </el-table-column>
      <el-table-column label="灌溉方式" align="center" prop="irrMode">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.irr_mode" :value="scope.row.irrMode"/>
        </template>
      </el-table-column> -->
      <el-table-column label="种植面积" align="center" prop="landArea">

      </el-table-column>
      <el-table-column label="实际水量" align="center" prop="landVol">

      </el-table-column>
      <el-table-column label="单价" align="center" prop="unitPrice">

      </el-table-column>
      <el-table-column label="水费" align="center" prop="waterFee">

      </el-table-column>
      <el-table-column label="核算人" align="center" prop="operId">

      </el-table-column>
      <el-table-column label="核算时间" align="center" prop="operTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.operTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="operMark">

      </el-table-column>
      <!-- <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['waterPrice:WiOrgfeeY:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['waterPrice:WiOrgfeeY:remove']"
          >删除</el-button>
        </template>
      </el-table-column> -->
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改水管机构水费核算对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" inline :model="form" :rules="rules" label-width="120px">
        <!-- <el-form-item label="地块性质" prop="landAttr">
          <el-select class="w202" v-model="form.landAttr" placeholder="请选择地块性质" clearable>
            <el-option
              v-for="dict in dict.type.land_attr"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="灌溉方式" prop="irrMode">
          <el-select class="w202" v-model="form.irrMode" placeholder="请选择灌溉方式" clearable>
            <el-option
              v-for="dict in dict.type.irr_mode"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item> -->
        <el-form-item label="种植面积" prop="landArea">
          <el-input v-model="form.landArea" placeholder="请输入种植面积" />
        </el-form-item>
        <el-form-item label="实际水量" prop="landVol">
          <el-input v-model="form.landVol" placeholder="请输入实际水量" />
        </el-form-item>
        <el-form-item label="单价" prop="unitPrice">
          <el-input v-model="form.unitPrice" placeholder="请输入单价" />
        </el-form-item>
        <el-form-item label="水费" prop="waterFee">
          <el-input v-model="form.waterFee" placeholder="请输入水费" />
        </el-form-item>
        <el-form-item label="核算人" prop="operId">
          <el-input v-model="form.operId" placeholder="请输入核算人" />
        </el-form-item>
        <el-form-item label="核算时间" prop="operTime">
          <el-date-picker clearable
            v-model="form.operTime"
            type="date"
            class="w202"
            value-format="yyyy-MM-dd"
            placeholder="请选择核算时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="备注" prop="operMark">
          <el-input v-model="form.operMark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listWiOrgfeeY, listWiFarmfeeY , listcaleFee , getWiOrgfeeY, delWiOrgfeeY, addWiOrgfeeY, updateWiOrgfeeY } from "@/api/waterPrice/WiOrgfeeY";
import { organtreeList,getFeeKind } from "@/api/common";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import { listWiFarmerB} from "@/api/datam/WiFarmerB";
export default {
  name: "WiOrgfeeY",
  dicts:["land_attr","irr_mode"],
  components:{Treeselect},
  data() {
    return {
      // 遮罩层
      loading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 水管机构水费核算表格数据
      WiOrgfeeYList: [],
      xzqhOptions:[],
      WiFarmerBList:[],
      feeKind:'1',
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orgCd:'ORG6543220000',
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    let day = new Date();
    let nowYear = day.getFullYear().toString();
    this.queryParams.year = nowYear;
    this.getFeeKindtype();
    this.getTreeselect()
  },
  methods: {
    // 获取收缴方式  0按农户 1按机构 year=2023
    getFeeKindtype(){
      getFeeKind({year: this.queryParams.year}).then(res => {
        this.feeKind = res.data.feeKind
      })
    },
    //水费核算
    caleFee(){
      if(!this.queryParams.orgCd){
        this.$message({
          message: '请选择水管机构',
          type: 'warning'
        });
        return
      }
      if(this.WiOrgfeeYList.length > 0){
        this.$message({
          message: '水费核定已完成！',
          type: 'warning'
        });
        return
      }
      listcaleFee({year: this.queryParams.year,orgCd:this.queryParams.orgCd}).then(res => {
          this.getList()
      })
    },
    /** 查询水管机构水费核算列表 */
    getList() {
      if(this.feeKind == 1){
        this.loading = true;
        listWiOrgfeeY( this.queryParams.year,this.queryParams.orgCd).then(response => {
          this.WiOrgfeeYList = response.data?[response.data]:[];
          // this.total = response.total;
          this.loading = false;
        });
      }else{
        if(!this.queryParams.farmCd){
          this.$message({
            message: '请选择农户',
            type: 'warning'
          });
          return
        }
        this.loading = true;
        listWiFarmfeeY(this.queryParams.year,this.queryParams.farmCd).then(response => {
          this.WiOrgfeeYList = response.data?[response.data]:[];
          // this.total = response.total;
          this.loading = false;
        });
      }
    },
    // 获取区划名称
    selectAd(val) {
      // this.queryParams.tableTitle = val.orgNm
      this.getlistWiFarmer()
    },
    /** 查询农户信息登记列表 */
    getlistWiFarmer() {
      listWiFarmerB({orgCd:this.queryParams.orgCd }).then(response => {
        this.WiFarmerBList = response.rows;
        setTimeout(() => this.getList(),300)
        if(this.feeKind == 0 ){
          this.queryParams.farmCd =  response.rows[0].farmCd
        }
      });
    },
    /** 查询行政区域下拉树结构 */
    getTreeselect() {
      organtreeList({}).then((response) => {
        this.xzqhOptions = this.handleTree(response.data, "orgCd", "parentOrgCd");
        this.getlistWiFarmer()
      });
    },
    /** 转换菜单数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.orgCd,
        label: node.orgNm,
        children: node.children,
      };
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        feeYr: null,
        orgCd: null,
        landAttr: null,
        irrMode: null,
        landArea: null,
        landVol: null,
        unitPrice: null,
        waterFee: null,
        operId: null,
        operTime: null,
        operMark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.feeYr)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加水管机构水费核算";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const feeYr = row.feeYr || this.ids
      // getWiOrgfeeY(feeYr).then(response => {
        this.form = row;
        this.open = true;
        this.title = "修改水管机构水费核算";
      // });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.feeYr != null) {
            updateWiOrgfeeY(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addWiOrgfeeY(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const feeYrs = row.feeYr || this.ids;
      this.$modal.confirm('是否确认删除水管机构水费核算编号为"' + feeYrs + '"的数据项？').then(function() {
        return delWiOrgfeeY(feeYrs);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('waterPrice/WiOrgfeeY/export', {
        ...this.queryParams
      }, `特定水价设定_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
